import unittest
from time import sleep
from parameterized import parameterized
import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from util import read_json


class TestRegisterLogin(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
        # 获取session对象
        self.session = requests.session()
        # 获取ApiRegisterLogin实例
        self.reg = ApiRegisterLogin(self.session)

    # 结束
    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()


    # # 1、获取图片验证码接口 测试
    # @parameterized.expand(read_json("register_login.json", "img_code"))
    # def test01_img_code(self,random,expect_code):
    #     try:
    #         # 1、调用图片验证码接口
    #         r = self.reg.api_img_code(random)
    #         # 2、查看响应状态码
    #         self.assertEqual(expect_code, r.status_code)
    #     except Exception as e:
    #         # 日志
    #         print(e)
    #         # 抛异常
    #         raise
    #
    # # 2、获取短信验证码接口 测试
    # @parameterized.expand(read_json("register_login.json", "phone_code"))
    # def test02_phone_code(self, phone, imgVerifyCode,expect_text):
    #     try:
    #         # 1、调用获取图片验证码接口 --目的：让session对象记录cookie
    #         self.reg.api_img_code(123)
    #         # 2、调用短信验证码接口
    #         r = self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
    #         # 3、查看响应结果
    #         self.assertIn(expect_text, r.text)
    #     except Exception as e:
    #         # 日志
    #         print(e)
    #         # 抛异常
    #         raise
    #
    #
    # # 3、注册接口 测试
    # @parameterized.expand(read_json("register_login.json", "register"))
    # def test03_register(self, phone, password, imgVerifyCode, phone_code, expect_text):
    #     try:
    #         # 1、图片验证码接口
    #         self.reg.api_img_code(123)
    #         # 2、短信验证码接口
    #         self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
    #         # 3、注册接口
    #         r = self.reg.api_register(phone=phone, password=password, verifycode=imgVerifyCode, phone_code=phone_code)
    #         # 4、查看结果
    #         self.assertIn(expect_text, r.text)
    #     except Exception as e:
    #         # 日志
    #         print(e)
    #         # 抛异常
    #         raise


    # 4、登录接口 测试
    @parameterized.expand(read_json("register_login.json", "login"))
    def test04_login(self, shop_id,username, password,expect_text):
        try:
            i = 1
            r = None
            if "error" in password:
                while i <= 3:
                    r = self.reg.api_login(shop_id,username, password)
                    i += 1
                # 断言锁定
                # print("测试锁定：",r.text)
                self.assertIn("锁定",r.text)
                # 暂停60秒
                sleep(60)
                # 测试登录成功
                r = self.reg.api_login(shop_id='1',username="zhangsan",password="123")
                log.info("执行接口结果为：{}".format(r.status_code))
                print()
                print(shop_id,username,password)
                # 断言登录成功
                self.assertIn(r.status_code,expect_text)
                log.info("执行断言通过!")
            else:
                pass
                # 1、调用登录接口
                r = self.reg.api_login(shop_id=shop_id,username=username,password=password)
                # 2、查看结果
                print()
                print(shop_id,username,password)
                print(self.reg.session.cookies.get_dict())
                log.info("执行接口结果为：{}".format(r.status_code))
                # self.assertIn(expect_text,r.response.text) # 断言文本
                self.assertEqual(r.status_code, expect_text) # 断言状态码
                log.info("执行断言通过!")
        except Exception as e:
            # 日志
            log.error("断言失败，原因：{}".format(e))
            # 抛异常
            raise

    @parameterized.expand(read_json("register_login.json", "web"))
    def test05_login_web(self,status,expect_text):
            try:
                if status == "已登录":
                    # 调用登录接口
                    r = self.reg.api_login_web()
                    # 调用查询登录状态接口
                    # r = self.reg.api_login_web()
                    log.info("执行接口结果为：{}".format(r.status_code))
                    # 看结果
                    self.assertEqual(r.status_code, expect_text)
                    # print(r.text)
                log.info("执行断言通过!")
            except Exception as e:
                # 日志
                print(e)
                # 抛异常
                raise

    @parameterized.expand(read_json("register_login.json", "logout"))
    def test06_logout(self,status,expect_text):
        try:
            if status == "退出":
                # 调用登录接口
                self.reg.api_login(shop_id='1', username="zhangsan", password="123")
            # 调用退出接口
            r = self.reg.api_logout()
            log.info("执行接口结果为：{}".format(r.status_code))
            # 看结果
            self.assertEqual(expect_text,r.status_code)
            log.info("执行断言通过!")
        except Exception as e:
            # 日志
            print(e)
            # 抛异常
            raise

    # # 5、查询登录状态接口 测试
    # @parameterized.expand(read_json("register_login.json", "login_status"))
    # def test05_login_status(self, status,expect_text):
    #     try:
    #         if status == "已登录":
    #             # 调用登录接口
    #             self.reg.api_login(keywords="13600001111", password="test123")
    #         # 调用查询登录状态接口
    #         r = self.reg.api_login_status()
    #         # 看结果
    #         self.assertIn(expect_text, r.text)
    #     except Exception as e:
    #         # 日志
    #         print(e)
    #         # 抛异常
    #         raise
